const Controller = require('egg').Controller;

const MenuVal = {
  parentId:{type:'integer',required:true},
  type:{ type:'integer',required:true },  //类型0是菜单,1是目录
  name:{type:'string',required:true}, //名字
  icon:{type:'string',required:true}, //图标
  path:{type:'string',required:true}, //访问路径
  component:{ type:'string',required:false},  //前端组件
  redirect:{type:'string',required:false}  //重定向
}
class Menu extends Controller {
  //查询所有
  async index() {
    const { ctx, service } = this;
    let data = await service.user.menu.findAll();
    return ctx.helper.success(ctx, '成功', data);
  }
  //:id查询单条
  async show() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    let data = await service.user.menu.findOne(id);
    return ctx.helper.success(ctx, '成功', data);
  }
  //新建菜单
  async create(){
    const { ctx,service } = this;
    ctx.validate(MenuVal,ctx.request.body);
    let data = await service.user.menu.create(ctx.request.body);
    if(data!==-1){
      ctx.helper.success(ctx,"成功",data)
    }else{
      ctx.helper.error(ctx,"失败",data);
    }
  }
  //更新菜单
  async update(){
    const { ctx,service } = this;
    ctx.validate(MenuVal,ctx.request.body);
    let data = await service.user.menu.update(ctx.request.body,ctx.params);
    if(data!==-1){
      ctx.helper.success(ctx,"成功",data);
    }else{
      ctx.helper.error(ctx,"失败",data);
    }
  }
  //删除菜单
  async destroy(){
    const { ctx,service } = this;
    console.log();
    if(ctx.params.id){
        let data = await service.user.menu.destroy(ctx.params.id);
        if(data!==-1){
          ctx.helper.success(ctx,"成功",data);
        }else{
          ctx.helper.error(ctx,"失败",data);
        }
    }else{
      ctx.helper.error(ctx,'缺少参数id',-1);
    }
  }
}

module.exports = Menu;  
